<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/11/27 0027
 * Time: 17:11
 */

namespace logicmodel;


use datamodel\BoxOrders;
use datamodel\Goods;
use datamodel\GoodsOrders;
use datamodel\OrdersBox;
use datamodel\OrdersGoods;
use datamodel\Users;
use datamodel\UsersBox;
use datamodel\UsersGoods;
use EasyWeChat\Factory;
use logicmodel\award\Recommend;
use think\Db;
use Yansongda\Pay\Pay;

class NotifyLogic
{

    public  $message='支付失败';
    public function __construct()
    {
    }

    /**
     * 商品APP回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function ordersAppGoodsNotify(){
        $config = [
            'app_id' => config('site.wx_app_id'), // APP APPID
            'secret' => config('site.wx_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );

        $response = $app->handlePaidNotify(function ($message, $fail) {
            $out_trade_no =  $message['out_trade_no'];
            $ordersGoodsData =  new OrdersGoods();
            $orderInfo = $ordersGoodsData->alias('og')
                ->join('users u','og.uid = u.id')
                ->where(['og.order_num'=>$out_trade_no,'og.status'=>0])
                ->field(['og.*','u.buy_goods_number','u.pid','u.nick_name'])
                ->find();
            if(empty($orderInfo)) return 'FAIL';
            if ($message['return_code'] === 'SUCCESS') {
                $data['status'] = 1;
                $data['pay_time'] = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $ordersGoodsData->updateByWhere(['id'=>$orderInfo['id']],$data);
                if($result < 0){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                $goods['uid'] = $orderInfo['uid'];
                $goods['goods_id'] = $orderInfo['goods_id'];
                $goods['price'] = $orderInfo['price'];
                $goods['create_time'] = date('Y-m-d H:i:s');
                $result = (new UsersGoods())->insertGetId($goods);
                if(!$result){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                Db::commit();
                (new Users())->where(['id'=>$orderInfo['uid']])->setInc('buy_goods_number',1);
                Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setInc('sales',1);
                Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setDec('surplus',1);
                if($orderInfo['buy_goods_number'] == 0) (new Recommend())->award($orderInfo['pid'],$orderInfo['uid'],'推荐会员'.$orderInfo['nick_name'].'完成交易奖励',2);
            } else {
                return $fail('通信失败，请稍后再通知我');
            }
        });
        return   $response->send();
    }

    /**
     * 微信盲盒APP回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function ordersAppBoxNotify(){
        $config = [
            'app_id' => config('site.wx_app_id'), // APP APPID
            'secret' => config('site.wx_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );
        $response = $app->handlePaidNotify(function ($message, $fail) {
            $out_trade_no =  $message['out_trade_no'];
            $ordersBoxData =  new OrdersBox();
            $orderInfo = $ordersBoxData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
            if(empty($orderInfo)) return 'FAIL';
            if ($message['return_code'] === 'SUCCESS') {
                $data['status'] = 1;
                $data['pay_time'] = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $ordersBoxData->updateByWhere(['id'=>$orderInfo['id']],$data);
                if($result < 0){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                //根据类型 增加盲盒
                $box['uid'] = $orderInfo['uid'];
                $box['box_id'] = $orderInfo['box_id'];
                $box['price'] = $orderInfo['price'];
                $box['create_time'] = date('Y-m-d H:i:s');
                $result = (new UsersBox())->insertGetId($box);
                if(!$result){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                Db::commit();
                 Db::name('box')->where(['id'=>$orderInfo['box_id']])->setInc('sales',1);
                Db::name('box')->where(['id'=>$orderInfo['box_id']])->setDec('surplus',1);
            } else {
                return $fail('通信失败，请稍后再通知我');
            }
        });
        return   $response->send();
    }

    /**
     * 商品公众号回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function ordersGlobalGoodsNotify(){
        $config = [
            'app_id' => config('site.wx_global_app_id'),
            'secret' => config('site.wx_global_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );
        $response = $app->handlePaidNotify(function ($message, $fail) {
            $out_trade_no =  $message['out_trade_no'];
            $ordersGoodsData =  new OrdersGoods();
            $orderInfo = $ordersGoodsData->alias('og')
                ->join('users u','og.uid = u.id')
                ->where(['og.order_num'=>$out_trade_no,'og.status'=>0])
                ->field(['og.*','u.buy_goods_number','u.pid','u.nick_name'])
                ->find();
            if(empty($orderInfo)) return 'FAIL';
            if ($message['return_code'] === 'SUCCESS') {
                $data['status'] = 1;
                $data['pay_time'] = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $ordersGoodsData->updateByWhere(['id'=>$orderInfo['id']],$data);
                if($result < 0){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                $goods['uid'] = $orderInfo['uid'];
                $goods['goods_id'] = $orderInfo['goods_id'];
                $goods['price'] = $orderInfo['price'];
                $goods['create_time'] = date('Y-m-d H:i:s');
                $result = (new UsersGoods())->insertGetId($goods);
                if(!$result){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                Db::commit();
                (new Users())->where(['id'=>$orderInfo['uid']])->setInc('buy_goods_number',1);
                Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setInc('sales',1);
                Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setDec('surplus',1);
                if($orderInfo['buy_goods_number'] == 0) (new Recommend())->award($orderInfo['pid'],$orderInfo['uid'],'推荐会员'.$orderInfo['nick_name'].'完成交易奖励',2);
            } else {
                return $fail('通信失败，请稍后再通知我');
            }
        });
        return   $response->send();
    }

    /**
     * 微信盲盒公众号回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function ordersGlobalBoxNotify(){
        $config = [
            'app_id' => config('site.wx_global_app_id'),
            'secret' => config('site.wx_global_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );
        $response = $app->handlePaidNotify(function ($message, $fail) {
            $out_trade_no =  $message['out_trade_no'];
            $ordersBoxData =  new OrdersBox();
            $orderInfo = $ordersBoxData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
            if(empty($orderInfo)) return 'FAIL';
            if ($message['return_code'] === 'SUCCESS') {
                $data['status'] = 1;
                $data['pay_time'] = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $ordersBoxData->updateByWhere(['id'=>$orderInfo['id']],$data);
                if($result < 0){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                //根据类型 增加盲盒
                $box['uid'] = $orderInfo['uid'];
                $box['box_id'] = $orderInfo['box_id'];
                $box['price'] = $orderInfo['price'];
                $box['create_time'] = date('Y-m-d H:i:s');
                $result = (new UsersBox())->insertGetId($box);
                if(!$result){
                    Db::rollback();
                    return $fail('通信失败，请稍后再通知我');
                }
                Db::commit();
                Db::name('box')->where(['id'=>$orderInfo['box_id']])->setInc('sales',1);
                Db::name('box')->where(['id'=>$orderInfo['box_id']])->setDec('surplus',1);
            } else {
                return $fail('通信失败，请稍后再通知我');
            }
        });
        return   $response->send();
    }



    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function goodsAppOrdersNotify(){
        $config = [
            'app_id' => config('site.wx_app_id'), // APP APPID
            'secret' => config('site.wx_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config);
        $response = $app->handlePaidNotify(function ($message, $fail) {
            if ($message['return_code'] === 'SUCCESS') {
                $out_trade_no =  $message['out_trade_no']; //订单编号
                $goodsOrdersData = new GoodsOrders();
                $orderInfo =  $goodsOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
                if(empty($orderInfo)) return 'FAIL';
                $time = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $goodsOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售作品','出售作品收益');
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $usersGoodsData = new UsersGoods();
                $result = $usersGoodsData->where(['id'=>$orderInfo['users_goods_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                $goods['uid'] = $orderInfo['buy_uid'];
                $goods['goods_id'] = $orderInfo['goods_id'];
                $goods['price'] = $orderInfo['price'];
                $goods['create_time'] = $time;
                $result = $usersGoodsData->insertGetId($goods);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                Db::commit();
            }
        });
        return   $response->send();
    }

    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function boxAppOrdersNotify(){
        $config = [
            'app_id' => config('site.wx_app_id'), // APP APPID
            'secret' => config('site.wx_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );
        $response = $app->handlePaidNotify(function ($message, $fail) {
            if ($message['return_code'] === 'SUCCESS') {
                $out_trade_no =  $message['out_trade_no']; //订单编号
                $boxOrdersData = new BoxOrders();
                $orderInfo =  $boxOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
                if(empty($orderInfo)) return 'FAIL';
                $time = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $boxOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售盲盒','出售盲盒收益');
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $usersBoxData = new UsersBox();
                $result = $usersBoxData->where(['id'=>$orderInfo['users_box_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                $box['uid'] = $orderInfo['buy_uid'];
                $box['box_id'] = $orderInfo['box_id'];
                $box['price'] = $orderInfo['price'];
                $box['create_time'] = $time;
                $result = $usersBoxData->insertGetId($box);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                Db::commit();
            }
        });
        return   $response->send();
    }



    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function goodsGlobalOrdersNotify(){
        $config = [
            'app_id' => config('site.wx_global_app_id'), // APP APPID
            'secret' => config('site.wx_global_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config);
        $response = $app->handlePaidNotify(function ($message, $fail) {
            if ($message['return_code'] === 'SUCCESS') {
                $out_trade_no =  $message['out_trade_no']; //订单编号
                $goodsOrdersData = new GoodsOrders();
                $orderInfo =  $goodsOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
                if(empty($orderInfo)) return 'FAIL';
                $time = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $goodsOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售作品','出售作品收益');
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $usersGoodsData = new UsersGoods();
                $result = $usersGoodsData->where(['id'=>$orderInfo['users_goods_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                $goods['uid'] = $orderInfo['buy_uid'];
                $goods['goods_id'] = $orderInfo['goods_id'];
                $goods['price'] = $orderInfo['price'];
                $goods['create_time'] = $time;
                $result = $usersGoodsData->insertGetId($goods);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                Db::commit();
            }
        });
        return   $response->send();
    }

    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function boxGlobalOrdersNotify(){
        $config = [
            'app_id' => config('site.wx_global_app_id'), // APP APPID
            'secret' => config('site.wx_global_app_secret'),
            'mch_id' => config('site.wx_mch_id'),
            'key' => config('site.wx_mch_key'),
        ];
        $app = Factory::payment( $config );
        $response = $app->handlePaidNotify(function ($message, $fail) {
            if ($message['return_code'] === 'SUCCESS') {
                $out_trade_no =  $message['out_trade_no']; //订单编号
                $boxOrdersData = new BoxOrders();
                $orderInfo =  $boxOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
                if(empty($orderInfo)) return 'FAIL';
                $time = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $boxOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售盲盒','出售盲盒收益');
                if(!$result) {
                    Db::rollback();
                    return 'FAIL';
                }
                $usersBoxData = new UsersBox();
                $result = $usersBoxData->where(['id'=>$orderInfo['users_box_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                $box['uid'] = $orderInfo['buy_uid'];
                $box['box_id'] = $orderInfo['box_id'];
                $box['price'] = $orderInfo['price'];
                $box['create_time'] = $time;
                $result = $usersBoxData->insertGetId($box);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                Db::commit();
            }
        });
        return   $response->send();
    }



    /**
     * 支付宝APP回调
     * @return string|\Symfony\Component\HttpFoundation\Response
     */
    public function aliOrdersAppGoodsNotify(){

        $config = [
            'use_sandbox' => false, // 是否使用沙盒模式
            'app_id'    => config('site.ali_app_id'),
            'sign_type' => 'RSA2', // RSA  RSA2
            // 支付宝公钥字符串
            'ali_public_key' => config('site.ali_public_key'),
            // 自己生成的密钥字符串
            'private_key' => config('site.ali_private_key'),
        ];
        $alipay = Pay::alipay($config);
        try{
            $ali = $alipay->verify();
            $out_trade_no = $ali['out_trade_no'];
            $ordersGoodsData =  new OrdersGoods();
            $orderInfo = $ordersGoodsData->alias('og')
                ->join('users u','og.uid = u.id')
                ->where(['og.order_num'=>$out_trade_no,'og.status'=>0])
                ->field(['og.*','u.buy_goods_number','u.pid','u.nick_name'])
                ->find();

            if(empty($orderInfo)) return 'FAIL';
            $data['status'] = 1;
            $data['pay_time'] = date('Y-m-d H:i:s');
            Db::startTrans();
            $result = $ordersGoodsData->updateByWhere(['id'=>$orderInfo['id']],$data);
            if($result < 0){
                Db::rollback();
                return 'FAIL';
            }
            $goods['uid'] = $orderInfo['uid'];
            $goods['goods_id'] = $orderInfo['goods_id'];
            $goods['price'] = $orderInfo['price'];
            $goods['create_time'] = date('Y-m-d H:i:s');
            $result = (new UsersGoods())->insertGetId($goods);
            if(!$result){
                Db::rollback();
                return 'FAIL';
            }
            Db::commit();
            (new Users())->where(['id'=>$orderInfo['uid']])->setInc('buy_goods_number',1);
            Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setInc('sales',1);
            Db::name('goods')->where(['id'=>$orderInfo['goods_id']])->setDec('surplus',1);
            if($orderInfo['buy_goods_number'] == 0) (new Recommend())->award($orderInfo['pid'],$orderInfo['uid'],'推荐会员'.$orderInfo['nick_name'].'完成交易奖励',2);
        }catch (\Exception $e) {
            $e->getMessage();
            return 'FAIL';
        }
        return $alipay->success()->send();
    }

    /**
     * 支付宝盲盒APP回调
     * @return string|\Symfony\Component\HttpFoundation\Response
     */
    public function aliOrdersAppBoxNotify(){
        $config = [
            'use_sandbox' => false, // 是否使用沙盒模式
            'app_id'    => config('site.ali_app_id'),
            'sign_type' => 'RSA2', // RSA  RSA2
            // 支付宝公钥字符串
            'ali_public_key' => config('site.ali_public_key'),
            // 自己生成的密钥字符串
            'private_key' => config('site.ali_private_key'),
        ];
        $alipay = Pay::alipay($config);
        try{
             $ali =  $alipay->verify();
            $out_trade_no =  $ali['out_trade_no'];
            $ordersBoxData =  new OrdersBox();
            $orderInfo = $ordersBoxData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
            if(empty($orderInfo)) return 'FAIL';

                $data['status'] = 1;
                $data['pay_time'] = date('Y-m-d H:i:s');
                Db::startTrans();
                $result = $ordersBoxData->updateByWhere(['id'=>$orderInfo['id']],$data);
                if($result < 0){
                    Db::rollback();
                    return 'FAIL';
                }
                //根据类型 增加盲盒
                $box['uid'] = $orderInfo['uid'];
                $box['box_id'] = $orderInfo['box_id'];
                $box['price'] = $orderInfo['price'];
                $box['create_time'] = date('Y-m-d H:i:s');
                $result = (new UsersBox())->insertGetId($box);
                if(!$result){
                    Db::rollback();
                    return 'FAIL';
                }
                Db::commit();
                Db::name('box')->where(['id'=>$orderInfo['box_id']])->setInc('sales',1);
                Db::name('box')->where(['id'=>$orderInfo['box_id']])->setDec('surplus',1);
        }catch (\Exception $e) {
            $e->getMessage();
            return 'FAIL';
        }
        return $alipay->success()->send();
    }

    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function aliGoodsAppOrdersNotify(){
        $config = [
            'use_sandbox' => false, // 是否使用沙盒模式
            'app_id'    => config('site.ali_app_id'),
            'sign_type' => 'RSA2', // RSA  RSA2
            // 支付宝公钥字符串
            'ali_public_key' => config('site.ali_public_key'),
            // 自己生成的密钥字符串
            'private_key' => config('site.ali_private_key'),
        ];
        $alipay = Pay::alipay($config);
        try{
            $ali = $alipay->verify();
            $out_trade_no = $ali['out_trade_no'];
            $goodsOrdersData = new GoodsOrders();
            $orderInfo =  $goodsOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
            if(empty($orderInfo)) return 'FAIL';
            $time = date('Y-m-d H:i:s');
            Db::startTrans();
            $result = $goodsOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
            if(!$result) {
                Db::rollback();
                return 'FAIL';
            }
            $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售作品','出售作品收益');
            if(!$result) {
                Db::rollback();
                return 'FAIL';
            }
            $usersGoodsData = new UsersGoods();
            $result = $usersGoodsData->where(['id'=>$orderInfo['users_goods_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
            if(!$result){
                Db::rollback();
                return 'FAIL';
            }
            $goods['uid'] = $orderInfo['buy_uid'];
            $goods['goods_id'] = $orderInfo['goods_id'];
            $goods['price'] = $orderInfo['price'];
            $goods['create_time'] = $time;
            $result = $usersGoodsData->insertGetId($goods);
            if(!$result){
                Db::rollback();
                return 'FAIL';
            }
            Db::commit();

        }catch (\Exception $e) {
            $e->getMessage();
            return 'FAIL';
        }
        return $alipay->success()->send();
    }

    /**
     * 交易市场作品回调
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function aliBoxAppOrdersNotify(){
        $config = [
            'use_sandbox' => false, // 是否使用沙盒模式
            'app_id'    => config('site.ali_app_id'),
            'sign_type' => 'RSA2', // RSA  RSA2
            // 支付宝公钥字符串
            'ali_public_key' => config('site.ali_public_key'),
            // 自己生成的密钥字符串
            'private_key' => config('site.ali_private_key'),
        ];
        $alipay = Pay::alipay($config);
        try{
            $ali = $alipay->verify();
            $out_trade_no = $ali['out_trade_no'];
            $boxOrdersData = new BoxOrders();
            $orderInfo =  $boxOrdersData->where(['order_num'=>$out_trade_no,'status'=>0])->find();
            if(empty($orderInfo)) return 'FAIL';
            $time = date('Y-m-d H:i:s');
            Db::startTrans();
            $result = $boxOrdersData->where(['id'=>$orderInfo['id']])->update(['status'=>1,'pay_time'=>$time]);
            if(!$result) {
                Db::rollback();
                return 'FAIL';
            }
            $result = (new AccountLogic())->addAccount($orderInfo['sale_uid'],1,$orderInfo['price'],'出售盲盒','出售盲盒收益');
            if(!$result) {
                Db::rollback();
                return 'FAIL';
            }
            $usersBoxData = new UsersBox();
            $result = $usersBoxData->where(['id'=>$orderInfo['users_box_id'],'status'=>3])->update(['status'=>4,'sales_time'=>$time]);
            if(!$result){
                Db::rollback();
                return 'FAIL';
            }
            $box['uid'] = $orderInfo['buy_uid'];
            $box['box_id'] = $orderInfo['box_id'];
            $box['price'] = $orderInfo['price'];
            $box['create_time'] = $time;
            $result = $usersBoxData->insertGetId($box);
            if(!$result){
                Db::rollback();
                return 'FAIL';
            }
            Db::commit();

        }catch (\Exception $e) {
            $e->getMessage();
            return 'FAIL';
        }
        return $alipay->success()->send();
    }

}